<?php
require_once('include/config.php');
require_once '../include/config.php';

//Pour supprimer une actualité
if (isset($_GET['id'])) {
    $id = htmlentities($_GET['id'], ENT_QUOTES);
    //On récupère l'image pour la supprimer
    $sql = 'SELECT `img` FROM `plante` WHERE `idplante`=' . $id;
    $result = @mysql_query($sql);
    $nbr_result = @mysql_num_rows($result);
    if ($nbr_result > 0) {
        $data = @mysql_fetch_array($result);
        if ($data['img'] != '') {
            //on supprime l'image
            unlink('../' . $data['img']);
        }
        $sql = 'DELETE FROM `plante` WHERE `idplante`=' . $id;
        $result = @mysql_query($sql);
        if ($result) {
            $msg = '<p class="msg done">L\'entrée est bien supprimée</p>';
        } else {
            $msg = '<p class="msg error">L\'entrée n\'est pas supprimée</p>';
        }
    } else {
        $msg = '<p class="msg error">L\entrée que vous souhaitez supprimer n\'existe pas</p>';
    }
}

//Affichage des sous tableaux des actu
//@sqlRequete SQL pour les actualités
//@typeErr = Pour la charte graphique msg warning|msg info|msg done|msg error
//@msgErr = Message si pas de résultat
function tabActualite($sql, $typeErr, $msgErr) {
    $pyjama = true;
    $result = @mysql_query($sql);
    $nbr_actu = @mysql_num_rows($result);
    if ($nbr_actu > 0) {
        $tabActu = '<table>
                    <tr>
                        <th>Titre</th>                       
                        <th>Texte</th>                        
                        <th style="width:25px;">&nbsp;</th>
                        <th style="width:25px;">&nbsp;</th>
                    </tr>';
        while ($data = @mysql_fetch_array($result)) {
            if ($pyjama)
                $tabActu .= '<tr><td>' . htmlspecialchars(stripcslashes($data['titre'])) . '</td>';
            else
                $tabActu .= '<tr class="bg"><td>' . htmlspecialchars(stripcslashes($data['titre'])) . '</td>';
            $tabActu .= '<td>' . tronque(stripcslashes(strip_tags($data['description'])), 60) . '</td>';            
            $tabActu .= '<td style="text-align: center;"><a href="plante.php?id=' . $data['idplante'] . '"><img src="design/cog.png" widht="16px" height="16px" alt="Modifier" /></a></td>';
            $tabActu .= '<td style="text-align: center;"><a href="#" onclick="if(confirm(\'Etes-vous sur de vouloir supprimer cette entrée ?\')) document.location.href=\'plante-list.php?id=' . $data['idplante'] . '\'"><img src="design/delete.png" widht="16px" height="16px" alt="Supprimer" /></a></td></tr>';
            $pyjama = !$pyjama;
        }
        $tabActu .= '</table>';
    } else {
        $tabActu = '<p class="' . $typeErr . '">' . $msgErr . '</p>';
    }
    return $tabActu;
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//FR" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="fr" lang="fr">
    <head>
        <meta http-equiv="content-type" content="text/html; charset=utf-8" />
        <meta http-equiv="content-language" content="en" />
        <meta name="robots" content="noindex,nofollow" />
        <link rel="stylesheet" media="screen,projection" type="text/css" href="css/reset.css" /> <!-- RESET -->
        <link rel="stylesheet" media="screen,projection" type="text/css" href="css/main.css" /> <!-- MAIN STYLE SHEET -->
        <link rel="stylesheet" media="screen,projection" type="text/css" href="css/2col.css" title="2col" /> <!-- DEFAULT: 2 COLUMNS -->
        <link rel="alternate stylesheet" media="screen,projection" type="text/css" href="css/1col.css" title="1col" /> <!-- ALTERNATE: 1 COLUMN -->
        <!--[if lte IE 6]><link rel="stylesheet" media="screen,projection" type="text/css" href="css/main-ie6.css" /><![endif]--> <!-- MSIE6 -->
        <link rel="stylesheet" media="screen,projection" type="text/css" href="css/style.css" /> <!-- GRAPHIC THEME -->

        <title>Administration</title>
    </head>

    <body>
        <div id="main">
            <!-- Tray -->
            <div id="tray" class="box">
                <p class="f-left box">
                    <!-- Switcher -->
                    <span class="f-left" id="switcher">
                        <a href="#" rel="1col" class="styleswitch ico-col1" title="Enlever le menu gauche"><img src="design/switcher-1col.gif" alt="1 Column" /></a>
                        <a href="#" rel="2col" class="styleswitch ico-col2" title="Menu gauche"><img src="design/switcher-2col.gif" alt="2 Columns" /></a>
                    </span>
                    <strong><?php echo $nomProjet; ?></strong>
                </p>
                <p class="f-right"><a href="<?php echo $site; ?>" target="_blank" id="logout"><span><strong>Voir le site</strong></span></a>&nbsp;&nbsp;&nbsp;&nbsp;<strong><a href="logout.php" id="logout">Quitter l'administration</a></strong></p>
            </div> <!--  /tray -->
            <hr class="noscreen" />

            <!-- Columns -->
            <div id="cols" class="box">

                <!-- Aside (Left Column) -->
                <div id="aside" class="box">
                    <?php
                    require_once('include/menu-gauche.php');
                    ?>
                </div> <!-- /aside -->

                <hr class="noscreen" />

                <!-- Content (Right Column) -->
                <div id="content" class="box">

                    <h1>Liste des plantes</h1>
                    <div id="bouton"><a href="plante.php" class="ml-button-5">Ajouter une plante</a></div>
                    <?php
                    //Gestion de tous les articles
                    $sql = 'SELECT * FROM `plante` ORDER BY UPPER(`titre`)';
                    echo tabActualite($sql, 'msg error', 'Aucune plante');
                    ?>
                    <!--/Onglets-->

                    <div id="bouton"><a href="plante.php" class="ml-button-5">Ajouter une plante</a></div>
                </div> <!-- /content -->

            </div> <!-- /cols -->

            <?php
            require_once ('include/footer.php');
            ?>

        </div> <!-- /main -->
        <script type="text/javascript" src="js/jquery.js"></script>
        <script type="text/javascript" src="js/switcher.js"></script>
        <script type="text/javascript" src="js/toggle.js"></script>
        <script type="text/javascript" src="js/ui.core.js"></script>
        <script type="text/javascript" src="js/ui.tabs.js"></script>
        <script type="text/javascript">
            $(document).ready(function() {
                $(".tabs > ul").tabs();
            });
        </script>
    </body>
</html>